package com.example.kafkademo.consumer;

import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class ConsumerTest {
    public static void main(String[] args) {
        //配置集合
        Map<String, Object> map = new HashMap<>();
        map.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "192.168.200.102:9092");
        map.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringDeserializer");
        map.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringDeserializer");

        map.put(ConsumerConfig.GROUP_ID_CONFIG, "atguigu");
        map.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
        //创建消费者对象
        KafkaConsumer<String, String> kafkaConsumer = new KafkaConsumer<String, String>(map);
        //订阅主题
        kafkaConsumer.subscribe(Collections.singleton("test"));
        //拉取数据

        while (true){
            ConsumerRecords<String, String> listConsumerRecord = kafkaConsumer.poll(100);

            for (ConsumerRecord<String, String> stringStringConsumerRecord : listConsumerRecord) {
                System.out.println("stringStringConsumerRecord = " + stringStringConsumerRecord);
            }
        }


        //kafkaConsumer.close();
    }
}
